-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dsa-week1-assignment 윤영서 #2
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
고생 많으셨습니다
const solution = (numbers, index = 0) => { | ||
if (index >= numbers.length) { | ||
return 0; | ||
} else { | ||
return numbers[index] + solution(numbers, index + 1); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아마 for문이 지금 제일 익숙해서 index를 이용해서 해결해주신 것 같아요. 빈 배열이 주어진 경우와, 배열이 하나씩 줄어둔다는 것을 이용할 수 있다는 것도 알아두시면 좋을 것 같습니다.
const solution = (numbers) => {
if (numbers.length === 0) {
return 0;
}
return numbers[0] + solution(numbers.slice(1));
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slice 메소드를 사용할 수 있다는건 생각도 안했네요 ㅠㅠ 감사합니다 ㅎㅎ
const solution = (n) => { | ||
let pre = 0; | ||
let curr = 1; | ||
let temp = 0; | ||
if (n <= -1) { | ||
return 0; | ||
} else if (n === 0 || n === 1) { | ||
return n; | ||
} | ||
while (n >= 2) { | ||
temp = pre; | ||
pre = pre + curr; | ||
curr = temp; | ||
n--; | ||
} | ||
return pre + curr; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
프로그램을 더 이상 실행할 필요가 없을 경우 빠르게 종료하도록 하면, 아래 코드를 더 이상 읽지 않아도 되기도 하고 불필요한 코드 실행도 막을 수 있어요. 게다가 진짜 코드의 의도는 아래에 배치하여 의도를 드러낼 수도 있습니다.
const solution = (n) => {
if (n <= -1) {
return 0;
}
if (n === 0 || n === 1) {
return n;
}
let pre = 0;
let curr = 1;
let temp = 0;
while (n >= 2) {
temp = pre;
pre = pre + curr;
curr = temp;
n--;
}
return pre + curr;
};
See also
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
얼리리턴을 생각해놓고는 할당을 하고 얼리리턴을 해줘버렸네요 ㅎㅎ 감사합니다!!
} | ||
|
||
while (exponent <= length - 1) { | ||
acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문자열 마지막 문자를 숫자로 변경한 후에 제곱을 더해주고 있는 것 같아요. 문자열 마지막이라면 index로 접근해도 좋을 것 같아요
acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent; | |
acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent; |
|
||
const length = n.length; | ||
if (length === 1) { | ||
return acc + parseInt(n, 10) * 2 ** exponent; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
연산자의 우선순위는 프로그래밍 언어마다 다를수도 있어요. 그래서 a + b + c같이 아주 자명한 경우가 아니라면 적절히 괄호로 묶어주는 것이 좋습니다.
return acc + parseInt(n, 10) * (2 ** exponent);
풀리퀘 많이 안해봐서 어렵네용,,,
앞으로 6주 잘 부탁드립니당~